<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Promise.then</title>
</head>
<body>
  <script>
    const loadImg = (src) => {
      return new Promise((resolve, reject) => {
        const img = new Image()
        img.src = src
        img.style.width = '100%'
        img.onload = () => resolve(img)
        img.onerror = reject
      })
    }
    const promise = loadImg('../img/bg.jpg')
    promise.then(res => {
      console.log(`width: ${res.width}, height: ${res.height}`)
      return res
    }).then(res => {
      document.body.appendChild(res)
    }).catch(err => {
      console.log(err.message || err)
    })
  </script>
</body>
</html>